#ifndef CNETWORKTHREAD_H
#define CNETWORKTHREAD_H

#include <QThread>
#include <QTimer>
#include <QTcpSocket>
#include <QUdpSocket>
#include "module/chttpmanger.h"
#include "module/csynctcpsocket.h"
#include "cmd_parse/ccmdparse.h"
#include "cexeccmd.h"
#include "v2/ctcpconnect_v2.h"

class CTcpConnect : public QObject
{
    Q_OBJECT
public:
    explicit CTcpConnect(QObject *parent = 0);
    ~CTcpConnect();
    void setUpKnxObjs();    
    void sendDataToServer(QString data);

public slots:

    void doConnectToServer();
    void doConnectToPushServer();
    void doDisconnectFromServer();
    void doRestart();
    void doStateCheck();

    void doConnected();
    void doDisconnect();

    void doGetProject();
    void doEndGetProject();

    void doTcpDataHandle(QString d);
    void doWriteTcpData(QByteArray &data);
    void doWriteTcpDataAppendSharp(QByteArray data);
    void doReadProjectSetting(QByteArray d);
    void doReadLocalUdpData();
    void doGatewayStatusUpdate(QString d);
    void doGatewayUpdateResult(QByteArray d);
    void doGatewayDeviceUpdateResult(QByteArray d);
    void doGatewayDeviceStatusUpdate(QString id, QString d);
    void doUpdateDeviceStatus();

    void doChangeUpdateMode(QString mode);
    void doControlCmd(QString cmd);
    void doPushDataHandle(QString d);

private:
    bool isSetUpPorject;
    CSyncTcpSocket *_tcpSocket,*_pushSocket;
    CCmdParse _cmdParse;
    QTimer _aliveTimer,_deviceUpdateTimer,_alldeviceUpdateTimer;
    QTime _lastAliveTime;

    CHttpManger *_http,*_httpUpdate,_httpStatusUpdate;

    QStringList _updateList;

    QMap<QString,QString> _updateMap,_allMap;
    int _index_all_map;
    bool _need_update_all;
    QString _updateMode;
};

class CNetworkThread : public QThread
{
    Q_OBJECT
public:
    explicit CNetworkThread(QObject *parent = 0);

    void checkUpdateVersion();
    void updateDownload(QString filename,int filesize);
    void updateDownloadWithId(QString downfileName, QString md5, int filesize);

    static bool gServerConnected;
public slots:
    void doRestartConnector();
    void doUpdateMessage(QByteArray data);
    void rebootDevice();

protected:
    void run();

private:
    CTcpConnect *tcpConn;
    CTcpConnect_v2 *tcpConn2;

    QUdpSocket *localControl;
    CExecCmd _execCmd;
    QStringList _argvlist;
    QTimer _rebootTimer;
    QTime _startTime;
    CHttpManger _httpUpdate;
};

#endif // CNETWORKTHREAD_H
